<?php

namespace app\airobot\controller;

use app\airobot\CommonLogic;
use app\airobot\model\BattleBox;
use app\airobot\model\OriginBattleConfig;
use app\airobot\model\OriginForge;
use app\airobot\model\OriginRobotSpinConfig;
use app\airobot\model\UserModel;
use think\admin\Controller;
use think\db\exception\DbException;
use think\facade\Db;
use ZipArchive;

/**
 * 超管系统-水池配置
 * Class Banner
 * @package app\airobot\controller
 */
class BattlePool extends Controller
{
    private $table = 'battle_pool';
    public $commonLogic;

    /**
     * 水池配置
     * @auth true
     */
    public function index()
    {
        $this->commonLogic = new CommonLogic();
        $this->title = '水池配置';
        $query = $this->_query($this->table);
        $query->page();
    }

    protected function _index_page_filter(array &$data)
    {
        $this->commonLogic = new CommonLogic();
        foreach ($data as &$vo) {
            $vo["total_amount"] = $this->commonLogic->DealAmount($vo['total_amount']);
            $vo["transfer_amount"] = $this->commonLogic->DealAmount($vo['transfer_amount']);
            $vo["operate_amount"] = $this->commonLogic->DealAmount($vo['operate_amount']);
            $vo["total_true_user_cost_amount"] = $this->commonLogic->DealAmount($vo['total_true_user_cost_amount']);
            $vo["total_true_user_win_amount"] = $this->commonLogic->DealAmount($vo['total_true_user_win_amount']);
            $vo["true_user_odds"] = number_format($vo['true_user_odds'], 4);
            $vo["system_profit_amount"] = $this->commonLogic->DealAmount($vo['system_profit_amount']);
        }
    }


    /**
     * 编辑数据字典
     * @auth true
     * @throws DbException
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            try {
                Db::startTrans();
                $data = $this->request->post();

                $battlePoolModel = new \app\airobot\model\BattlePool();
                $battlePool = $battlePoolModel->queryInfo(["id" => $data["id"]]);

                // 变化的金额
                $changePoolAmount = $battlePool["total_amount"] - $data['total_amount'];

                // 更新水池
                $updateData = [];
                $updateData["total_amount"] = $data['total_amount'];
                $updateData["operate_amount"] = $battlePool['operate_amount'] + $changePoolAmount;
                Db::table('battle_pool')->where(['id' => $data["id"]])->update($updateData);

                // 创建任务配置
                $createData["battle_pool_id"] = $data["id"];
                $createData["mode"] = $battlePool["mode"];
                $createData["start_pool_total_amount"] = $battlePool["total_amount"];
                $createData["end_pool_total_amount"] = $data['total_amount'];
                $createData["change_amount"] = $changePoolAmount;
                Db::table("battle_pool_operate_records")->insert($createData);

                // 如果一切正常，提交事务
                Db::commit();

                $this->success("保存成功！");
            } catch (DbException $e) {
                error_log(var_export($e->getMessage(), true) . "\n", 3, "/tmp/qp.log");
                Db::rollback();
            }

        }
        $this->commonLogic = new CommonLogic();
        $battlePoolModel = new \app\airobot\model\BattlePool();
        $vo = $battlePoolModel->queryInfo(["id" => $_GET["id"]]);
        $vo["total_amount"] = (int)$vo['total_amount'];
        $this->vo = $vo;
        OriginForge::mForm('form');
    }
}